$(function () {
  let Q = { pagenum: 1, pagesize: 5, cate_id: "", state: "" };
  initList();
  function initList() {
    axios({
      method: "GET",
      url: "/my/article/list",
      params: Q,
    }).then((res) => {
      if (res.data.status !== 0) {
        return layui.layer.msg(res.data.message);
      }
      let arr = [];
      res.data.data.forEach((element) => {
        arr.push(`
            <tr>
            <td>${element.title}</td>
            <td>${element.cate_name}</td>
            <td>${element.pub_date}</td>
            <td>${element.state}</td>
            <td>
                <button data-id="${element.Id}" type="button" class="layui-btn btn1 layui-btn-xs">编辑</button>
                <button data-id="${element.Id}" type="button" class="layui-btn btn2 layui-btn-xs layui-btn-danger">删除</button>
            </td>
        </tr>
            `);
        $("tbody").empty().html(arr);
      });
      Render(res.data.total);

      //   layui.use(["laypage", "layer"], function () {
      //     var laypage = layui.laypage;
      //     //完整功能
      //     laypage.render({
      //       elem: "demo7",
      //       count: res.data.total,
      //       limit: Q.pagesize,
      //       groups: 3,
      //       layout: ["count", "limit", "prev", "page", "next", "skip"],
      //       limits: [2, 3, 4, 5],
      //       jump: function (obj) {
      //         console.log(obj.curr);
      //         console.log(obj.limit);
      //         let pagenum = obj.curr;
      //         // initList();
      //       },
      //     });
      //   });
    });
  }

  initArtList();
  function initArtList() {
    axios({
      method: "GET",
      url: "/my/article/cates",
    }).then((res) => {
      // console.log(res);
      if (res.data.status !== 0) {
        return layui.layer.msg(res.data.message);
      }
      let arr = ['<option value="">请选择文章分类</option>'];
      res.data.data.forEach((ele) => {
        arr.push(`
                    <option value=${ele.Id}>${ele.name}</option>
                `);
      });
      let str = arr.join("");
      $("[name=cate_id]").empty().html(str);
      layui.form.render();
    });
  }

  $("#screenArticle").on("submit", function (e) {
    e.preventDefault();
    Q.cate_id = $("[name=cate_id]").val();
    Q.state = $("[name=state]").val();
    console.log(Q.cate_id, Q.state);
    initList();
  });

  function Render(total) {
    var laypage = layui.laypage;
    //完整功能
    laypage.render({
      elem: "demo7",
      count: total,
      limit: Q.pagesize,
      curr: Q.pagenum,
      layout: ["count", "limit", "prev", "page", "next", "skip"],
      limits: [2, 3, 4, 5],
      jump: function (obj, first) {
        // console.log(obj.curr);
        if (!first) {
          Q.pagenum = obj.curr;
          Q.pagesize = obj.limit;
          initList();
        }
      },
    });
  }

  $("tbody").on("click", ".btn1", function (e) {
    let index = $(this).attr("data-id");
    axios({
      method: "POST",
      url: "/my/article/updatecate",
      data: $(this).serialize(),
    }).then((res) => {
      if (res.data.status !== 0) {
        return layui.layer.msg(res.data.message);
      }
      layui.layer.msg("修改成功");
      initArt();
      layer.close(indexEdit);
    });
  });

  $("tbody").on("click", ".btn2", function () {
    let index = $(this).attr("data-id");
    axios({
      method: "GET",
      url: "/my/article/delete/" + index,
    }).then((res) => {
      if (res.data.status !== 0) {
        return layui.layer.msg(res.data.message);
      }
      if ($(".btn2").length == 1 && Q.pagenum > 1) {
        Q.pagenum--;
      }
      initList();
      layui.layer.msg("删除成功");
    });
  });
});
